<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="来源店铺">
                <a-select v-model="queryParam.from_store_id" allowClear :filter-option="filterOption" show-search>
                  <a-select-option :value="item.id" v-for="item in storeList" :key="item.id">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="使用店铺">
                <a-select v-model="queryParam.to_store_id" allowClear :filter-option="filterOption" show-search>
                  <a-select-option :value="item.id" v-for="item in storeList" :key="item.id">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="复制类型">
                <a-select v-model="queryParam.type" allowClear>
                  <a-select-option value="home"> 商城首页 </a-select-option>
                  <a-select-option value="category"> 商品分类 </a-select-option>
                  <a-select-option value="product"> 商城商品 </a-select-option>
                  <a-select-option value="center"> 个人中心 </a-select-option>
                  <a-select-option value="all"> 全部 </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="">
                <a-button type="primary" @click="getList()">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :border="false">
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }"
        size="default"
        :columns="columns"
        :data-source="list"
        :customRow="customRow"
        :scroll="{ x: 'max-content' }"
        :pagination="{
          total: this.total,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
      >
        <span slot="index" slot-scope="text, record">
          {{ isUse('home', record.type) }}
        </span>

        <span slot="cat" slot-scope="text, record">
          {{ isUse('category', record.type) }}
        </span>

        <span slot="goods" slot-scope="text, record">
          {{ isUse('product', record.type) }}
        </span>

        <span slot="center" slot-scope="text, record">
          {{ isUse('center', record.type) }}
        </span>

        <span slot="footnav" slot-scope="text, record">
          {{ isUse('footnav', record.type) }}
        </span>

        <span slot="delete_goods" slot-scope="text, record">
          {{ isUse('delete_goods', record.type) }}
        </span>

        <span slot="delete_cat" slot-scope="text, record">
          {{ isUse('delete_cat', record.type) }}
        </span>

        <span slot="finished" slot-scope="text, record">
          <a-badge :color="getStatus(record.finished)[1]" :text="getStatus(record.finished)[0]" />
        </span>

        <span slot="created_at" slot-scope="text, record">
          {{ record.created_at > 0 ? $moment(record.created_at * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { StoreCopyList } from '@/api/saas/copy'
import { wechatMpCardList } from '@/api/wechartVipCard'

const columns = [
  {
    title: '来源店铺',
    dataIndex: 'from_store_name',
  },
  {
    title: '使用店铺',
    dataIndex: 'to_store_name',
  },
  {
    title: '商城首页',
    scopedSlots: {
      customRender: 'index',
    },
  },
  {
    title: '商品分类',
    scopedSlots: {
      customRender: 'cat',
    },
  },
  {
    title: '商城商品',
    scopedSlots: {
      customRender: 'goods',
    },
  },
  {
    title: '个人中心',
    scopedSlots: {
      customRender: 'center',
    },
  },
  {
    title: '底部菜单',
    scopedSlots: {
      customRender: 'footnav',
    },
  },
  {
    title: '删除之前产品',
    scopedSlots: {
      customRender: 'delete_goods',
    },
  },
  {
    title: '删除之前分类',
    scopedSlots: {
      customRender: 'delete_cat',
    },
  },
  {
    title: '状态',
    scopedSlots: {
      customRender: 'finished',
    },
  },
  {
    title: '操作时间',
    dataIndex: 'created_at',
    scopedSlots: {
      customRender: 'created_at',
    },
    sorter: (a, b) => a.created_at - b.created_at,
  },
]

export default {
  data() {
    return {
      total: 0,
      pageNo: 1,
      pageSize: 10,
      columns,
      queryParam: {
        to_store_id: '',
        from_store_id: '',
        type: '',
      },
      list: [],
      selectedRowKeys: [],
      storeList: [],
    }
  },

  async mounted() {
    const res = await wechatMpCardList()
    if (res.code == 0) {
      this.storeList = res.store_list || []
    }
    this.getList()
  },

  computed: {
    getStatus() {
      return (status) => {
        let text, color
        switch (status) {
          case '0':
            text = '未完成'
            color = 'rgba(0, 0, 0, 0.25)'
            break

          case '1':
            text = '已完成'
            color = '#65C817'
            break
        }
        return [text, color]
      }
    },

    isUse() {
      return (el, type) => {
        if (type.indexOf('all') !== -1) {
          return '已使用'
        } else if (type.indexOf(el) !== -1) {
          return '已使用'
        } else {
          return '未使用'
        }
      }
    },
  },

  methods: {
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },

    // 获取数据列表
    async getList(pageNo = 1) {
      const hide = this.$message.loading('加载中', 0)
      let { queryParam, pageSize } = this
      const res = await StoreCopyList({
        ...queryParam,
        pageSize,
        pageNo,
      })
      if (res.code == 0) {
        this.selectedRowKeys = []
        let { totalCount, list } = res.data
        this.total = totalCount
        list.forEach((el) => {
          el.type = JSON.parse(el.type)
        })
        this.list = list
      }
      hide()
    },

    // 分页请求
    tablePageChange(e) {
      this.getList(e)
    },

    // 选中多行
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
  },
}
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 0 18px 0;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}

.content {
  display: flex;
  align-items: center;
  height: 48px;
  max-width: 300px;
  overflow: hidden;

  .txt {
    margin-left: 16px;
    flex: 1;
    overflow: hidden;

    * {
      margin: 0;
      font-size: 14px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }

    h3 {
      color: rgba(0, 0, 0, 0.85);
    }

    p {
      color: rgba(0, 0, 0, 0.45);
    }
  }
}

::v-deep {
  .ant-tag {
    cursor: pointer;
  }
}
</style>
